package com.yanwq.nwa.im;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dodoca_android on 2017/3/31.
 */

public class EventMgr {
    static private EventMgr instance;

    synchronized static private void syncInit() {
        if (instance == null) {
            instance = new EventMgr();
        }
    }

    static public EventMgr getInstance() {
        syncInit();
        return instance;
    }

    volatile private List<EventCallback> eventCallbackList;

    private EventMgr() {
        eventCallbackList = new ArrayList<>();
    }

    public void registerEventCallback(EventCallback callback) {
        if(!eventCallbackList.contains(callback)) {
            eventCallbackList.add(callback);
        }
    }

    public void unregisterEventCallback(EventCallback callback) {
        eventCallbackList.remove(callback);
    }

    public void triggerSuccess(String text) {
        for (EventCallback callback : eventCallbackList) {
            callback.onSuccess(text);
        }
    }

    public void triggerFailure(EventCallback.Failure failure) {
        for (EventCallback callback : eventCallbackList) {
            callback.onFailure(failure);
        }
    }
}
